//4.代理模式典型应用之缓存代理
let count = 0;
function  factorial(num:number):any {
  count++;
  if (num <= 1) return 1
  return num*factorial(num - 1);
};

console.log('factorial1:', factorial(3),'count:',count);
console.log('factorial1:', factorial(6),'count:',count);

//代理缓存
function proxy(fn:any) {
  let cache = {};
  return function (num:any){
    if (num in cache) {
      return cache[num]
    } else {
      return cache[num] = fn(num);
    }
  }
}
let fn2 = proxy(factorial);

// console.log('factorial2:', fn2(3), 'count:', count);
// console.log('factorial2:',fn2(6),'count:',count);
